package com.liuaq.liucomputer;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

public class SettingActivity extends BaseActivity implements View.OnClickListener {
    Button back;
    //根据ID获取到开关按钮
    Switch swtTheme;
    TextView themeTx;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        back = findViewById(R.id.back);
        themeTx = findViewById(R.id.theme_text);
        swtTheme = findViewById(R.id.sw_status);
        back.setOnClickListener(this);

        SharedPreferences sharedPreferences = getSharedPreferences("info", MODE_PRIVATE);
        boolean theme = sharedPreferences.getBoolean("ischecked",false);
        swtTheme.setChecked(theme);
        if(!theme){
            // 白
            themeTx.setText("简约白");
        }else {
            themeTx.setText("炫酷黑");
        }
        //给开关按钮设置监听状态改变事件
        swtTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                // Log.d("test", "onCheckedChanged: " + b);
                // 改变主题
                changeTheme(b);
            }
        });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // c
            case R.id.back:
                finish();
        }
    }
    /**
     * 改变主题
    * */
    private void changeTheme(boolean b) {
        SharedPreferences sharedPreferences = getSharedPreferences("info", MODE_PRIVATE);
        boolean theme = sharedPreferences.getBoolean("ischecked", false);
        if (theme) { //黑
            editor = sharedPreferences.edit();
            editor.putBoolean("ischecked",b);
            editor.apply();
            onResume();
        } else {
            editor = sharedPreferences.edit();
            editor.putBoolean("ischecked",b);
            editor.apply();
            onResume();
        }
    }
}